import java.util.Scanner;

public class MovieService {
    private Scanner scanner;

    public MovieService(Scanner scanner) {
        this.scanner = scanner;
    }

    // 添加影片
    public boolean addMovie() {
        System.out.print("请输入影片ID: ");
        String id = scanner.nextLine();

        // 检查ID是否已存在
        if (CinemaData.findMovieById(id) != null) {
            System.out.println("影片ID已存在！");
            return false;
        }

        System.out.print("请输入影片名称: ");
        String name = scanner.nextLine();
        System.out.print("请输入影片时长 (如120min): ");
        String duration = scanner.nextLine();
        System.out.print("请输入影片类型: ");
        String type = scanner.nextLine();
        System.out.print("请输入影片票价: ");
        double price = 0;
        try {
            price = Double.parseDouble(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("票价输入错误！");
            return false;
        }

        Movie newMovie = new Movie(id, name, duration, type, price);
        if (CinemaData.addMovie(newMovie)) {
            System.out.println("影片添加成功！");
            return true;
        } else {
            System.out.println("影片添加失败，系统影片已达上限！");
            return false;
        }
    }

    // 修改影片信息
    public boolean updateMovie() {
        System.out.print("请输入要修改的影片ID: ");
        String id = scanner.nextLine();

        Movie movie = CinemaData.findMovieById(id);
        if (movie == null) {
            System.out.println("影片不存在！");
            return false;
        }

        System.out.println("当前影片信息: " + movie);
        System.out.print("请输入新的影片名称 (不修改按回车): ");
        String name = scanner.nextLine();
        if (!name.isEmpty()) {
            movie.setName(name);
        }

        System.out.print("请输入新的影片时长 (不修改按回车): ");
        String duration = scanner.nextLine();
        if (!duration.isEmpty()) {
            movie.setDuration(duration);
        }

        System.out.print("请输入新的影片类型 (不修改按回车): ");
        String type = scanner.nextLine();
        if (!type.isEmpty()) {
            movie.setType(type);
        }

        System.out.print("请输入新的影片票价 (不修改按回车): ");
        String priceStr = scanner.nextLine();
        if (!priceStr.isEmpty()) {
            try {
                movie.setPrice(Double.parseDouble(priceStr));
            } catch (NumberFormatException e) {
                System.out.println("票价输入错误，未更新票价！");
            }
        }

        System.out.println("影片信息更新成功！");
        return true;
    }

    // 删除影片
    public boolean deleteMovie() {
        System.out.print("请输入要删除的影片ID: ");
        String id = scanner.nextLine();

        // 检查是否有关联的场次
        Schedule[] schedules = CinemaData.getSchedulesByMovieId(id);
        if (schedules.length > 0) {
            System.out.println("无法删除影片，该影片存在排片记录！");
            return false;
        }

        if (CinemaData.deleteMovie(id)) {
            System.out.println("影片已删除！");
            return true;
        } else {
            System.out.println("删除失败，影片不存在！");
            return false;
        }
    }

    // 查找影片
    public void findMovie() {
        System.out.print("请输入要查找的影片ID或名称: ");
        String keyword = scanner.nextLine();

        Movie[] movies = CinemaData.getAllMovies();
        boolean found = false;

        System.out.println("\n=== 查找结果 ===");
        for (Movie movie : movies) {
            if (movie.getId().contains(keyword) || movie.getName().contains(keyword)) {
                System.out.println(movie);
                found = true;
            }
        }

        if (!found) {
            System.out.println("未找到匹配的影片！");
        }
    }

    // 列出所有影片
    public void listAllMovies() {
        Movie[] movies = CinemaData.getAllMovies();
        System.out.println("\n=== 影片列表 ===");
        for (Movie movie : movies) {
            System.out.println(movie);
        }
    }
}
